//获取登录数据和购物车数据
//监听页面加载事件
document.addEventListener("DOMContentLoaded", function () {
    //创建购物车数据对象
    let cart_data = {
        user: '',
        cart: '',
        allPrice: 0.00,
    }

    let header = document.querySelector('.header');

    //获取登录数据
    if (localStorage.getItem('uid')) {
        //如果登录了，则保存uid到购物车对象
        cart_data.user = localStorage.getItem('uid');
        // console.log(cart_data);
        //获取购物车数据
        ajax({
            url: '/pro/cart_detail',
            type: 'get',
            data: cart_data.user,
            success: function (data) {
                data = JSON.parse(data);
                //购物车数据不为空
                if (data != null) {
                    //则对数据进行加工
                    let allPrice = 0;
                    for (let i = 0; i < data.length; i++) {
                        //获取商品的总价
                        allPrice += parseInt(data[i].price)*parseInt(data[i].pnum);
                    }
                    allPrice = allPrice.toFixed(2);
                    //将加工后的数据保存进购物车对象
                    cart_data.cart = data;
                    cart_data.allPrice = allPrice;
                    // console.log(cart_data);
                    header.innerHTML = template('cart', cart_data);

                    getButton();
                } else {
                    header.innerHTML = template('cart', cart_data);
                }
            },
            error: function (e) {
                console.log(e);
            }
        });
    } else {
        header.innerHTML = template('cart', cart_data);
    }
});

//根据购物车id删除商品
function cart_delete(id) {
    if (confirm('确定删除该商品吗？')) {
        ajax({
            url: '/pro/cart_delete',
            type: 'get',
            data: id,
            success: function (data) {
                alert('删除成功！');
                location.reload();
            },
            error: function (e) {
                console.log(e);
            }
        });
    }
}

//获取按钮，进行添加事件
function getButton() {
    var btns = document.getElementsByClassName('btns');
    var zongjia = document.getElementById("zongjia");

    for (var btn of btns) {
        //为每个按钮添加点击事件
        btn.onclick = function () {
            // console.log(btn);
            //选中span
            var span = this.parentElement.children[1];
            n = parseInt(span.innerHTML);
            if (this.innerHTML == "+") {
                n++;
            } else if (n > 1) {
                n--;
            }
            span.innerHTML = n;

            //计算总价
            var total = 0;
            //选中页面中的价格元素
            var prices = document.querySelectorAll('.main .prices');
            for (let i = 0; i < prices.length; i++) {
                // 对每个元素进行加工
                //截取掉获取到的价格前的¥符号
                var price = parseInt(prices[i].innerHTML.slice(1));
                //获取因按钮点击所改变的数量
                var num = document.querySelectorAll('.btn>span')[i].innerHTML;
                num = parseInt(num);
                //进行累加，计算总价
                total += (price * num);
            }
            zongjia.innerHTML = `¥${total.toFixed(2)}`;
        }
    }
}

//去购买页
function go_buy() {
    //获取隐藏input中保存的商品id->pid
    let cart_ids = document.querySelectorAll('.main>input[type=hidden]');

    for (let i = 0; i < cart_ids.length; i++) {
        //循环为每个商品保存到订单数据库
        let cart_id = cart_ids[i].value;
        //获取对应下标的商品数量
        let cart_nums = document.querySelectorAll('.main>.btn>span');
        let cart_num = cart_nums[i].innerHTML;
        //更新订单商品的数量
        ajax({
            url: '/pro/cart_num',
            type: 'post',
            data: `cart_id=${cart_id}&pnum=${cart_num}`,
            success: function(data){
                // console.log(data);
            },
            error: function(e){
                console.log(e);
            }
        });
    }
    // 获取总价
    let zongjia = document.getElementById("zongjia").innerHTML;
    zongjia = zongjia.slice(1);
    //将总价保存起来
    localStorage.setItem('allPrice',zongjia);
    location.href = '/order.html';
}
